package com.example.controller;

import com.example.anno.LogOperation;
import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.juli.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
//@Slf4j
public class DeptController {
    private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result list() {
//        System.out.println("查询全部部门数据");
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
    @LogOperation
    @DeleteMapping
    public Result delete(Integer id){
//        System.out.println("根据id删除部门,id="+id);
        log.info("根据id删除部门,id:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Dept dept){
//        System.out.println("新增部门:dept="+dept);
        log.info("新增部门:dept:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
//        System.out.println("根据id查询:id= " +id);
        log.info("根据id查询:id:{}" ,id);
        Dept dept= deptService.getById(id);
        return Result.success(dept);
    }
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门:dept="+dept);
        log.info("修改部门:dept:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
